package com.company.cloud.crm.modules.app.controller;

import java.util.*;

import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.crm.modules.app.controller.req.CustomerAppQuery4PageParam;
import com.company.cloud.crm.modules.base.entity.CustomerInfo;
import com.company.cloud.crm.modules.cache.models.CustomerModel;
import com.company.cloud.crm.modules.cache.utils.CrmBizChangeUtils;
import com.company.cloud.crm.modules.mgr.mapper.CustomerContactMgrMapper;
import com.company.cloud.crm.modules.mgr.service.CustomerContactMgrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

@Login
@RestController
@RequestMapping("/app/crm/customer")
@Api(tags="CRM_App_客户服务")
public class CustomerAppController {

	@Autowired
	private CustomerContactMgrService customerContactMgrService;
	@Resource
	private CustomerContactMgrMapper customerContactMgrMapper;
	@Autowired
	private UserRemoteService userRemoteService;

	@PostMapping("/queryCustMyUser")
	@ApiOperation(value="根据联络人（用户）查询客户信息", response=CustomerModel.class)
	public BaseResult queryCustMyUser(@RequestBody CustomerAppQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		List<CustomerInfo> list= customerContactMgrMapper.queryCustMyUserList(new QueryWrapper<CustomerInfo>()
				 .eq("a.rows_state", EnabledEnum.ENABLED.getCode())
				 .eq("b.contact_user_id", userInfo.getId())
				 .eq(StringUtils.hasText(params.getName()), "a.name", params.getName())
				 .orderByDesc("a.name")
				 );
		
		if(CollectionUtils.isEmpty(list)) {
			return BaseResult.ok();
		}

		Set<Integer> userIds=new HashSet<>();
		list.forEach(entity->{
			if(CheckUtils.hasInt(entity.getUserId())){
				userIds.add(entity.getUserId());
			}
		});
		List<CustomerModel> data=new ArrayList<CustomerModel>();
		if(userIds.size()>0){
			BaseClientResult<Map<Integer, UserSimple>> userSimpleMap = userRemoteService.getUserMapByIds(userIds);
			list.forEach(entity->{
				CustomerModel customerModel = CrmBizChangeUtils.getCustomer2Py(entity);
				if(CheckUtils.hasInt(entity.getUserId())){
					if(CheckUtils.hasClientResult(userSimpleMap)){
						if(!CollectionUtils.isEmpty(userSimpleMap.getData())){
							customerModel.setUser(userSimpleMap.getData().get(entity.getUserId()));
						}
					}
				}
				data.add(customerModel);
			});
		}else{
			list.forEach(entity->{
				data.add(CrmBizChangeUtils.getCustomer2Py(entity));
			});
		}



		//根据姓名首字母排序
		Collections.sort(data, new Comparator<CustomerModel>() {
			public int compare(CustomerModel o1, CustomerModel o2) {
				return o1.getNameFirstPy().compareToIgnoreCase(o2.getNameFirstPy());
			}
		});
		return BaseResult.ok(data);
		
	}

}
